package edu.umassd.hciproject;

import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;

public class StoreMapActivity extends Activity implements
		DialogInterface.OnClickListener, OnItemSelectedListener {

	ArrayList<String> stores;
	ImageView map;
	TextView tvMapName;
	Spinner spinner;
	MenuItem iNext, iPrevious;
	int currentMap = 0;
	boolean first = true;
	String[] path = {"Short path","Long path"};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_store_map);

		stores = (ArrayList<String>) getIntent().getExtras()
				.getStringArrayList(StoreSearchActivity.STORES_SELECTION_KEY);
		map = (ImageView) findViewById(R.id.ivStoreMap);
		map.setOnClickListener(new View.OnClickListener() {
			
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (first)
					showNext();
				else
					showPrevious();
			}
		});
		if (stores != null) {
			tvMapName = (TextView) findViewById(R.id.tvMapName);
			if (stores.contains("Target")) {
				map.setImageResource(R.drawable.target_map);
				currentMap = R.drawable.target_map;
				tvMapName.setText("Target");
			} else {
				AlertDialog.Builder builder = new AlertDialog.Builder(this);
				builder.setTitle("Sorry");
				builder.setMessage("Store maps not found");
				builder.setPositiveButton("Cancel", this);
			}
		}
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(
				StoreMapActivity.this, android.R.layout.simple_list_item_1, path);
		spinner = (Spinner) findViewById(R.id.spdistance);
		spinner.setAdapter(adapter);
		spinner.setOnItemSelectedListener(this);

	}

	protected void showPrevious() {
		// TODO Auto-generated method stub
		map.setImageResource(currentMap);
		first = true;
		updateMenu();
	}

	protected void showNext() {
		// TODO Auto-generated method stub
		map.setImageResource(R.drawable.target_path_short_refresh);
		first = false;
		updateMenu();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_store_map, menu);
		iPrevious = menu.findItem(R.id.mPrevious);
		iNext = menu.findItem(R.id.mNext);
		iPrevious.setEnabled(!first);
		iNext.setEnabled(first);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		//iPrevious = (MenuItem) findViewById(R.id.mPrevious);
		//iNext = (MenuItem) findViewById(R.id.mNext);
		switch (item.getItemId()) {
		case R.id.mPrevious:
			showPrevious();
			break;
		case R.id.mNext:
			showNext();
		}
		return false;
	}

	private void updateMenu() {
		// TODO Auto-generated method stub
		if ((iPrevious != null)&&(iNext != null)) {
			iPrevious.setEnabled(!first);
			iNext.setEnabled(first);
		}
		
	}

	@Override
	public void onClick(DialogInterface dialog, int which) {
		// TODO Auto-generated method stub
		dialog.dismiss();
		this.finish();
	}

	@Override
	public void onItemSelected(AdapterView<?> arg0, View arg1, int pos,
			long arg3) {
		// TODO Auto-generated method stub
		//int position = spinner.getSelectedItemPosition();
		switch (pos) {
		case 0:
			map.setImageResource(R.drawable.target_path_short);
			currentMap = R.drawable.target_path_short;
			first = true;
			updateMenu();
			break;
		case 1:
			map.setImageResource(R.drawable.target_path_long);
			currentMap = R.drawable.target_path_long;
			first = true;
			updateMenu();
			break;
		}
	}

	@Override
	public void onNothingSelected(AdapterView<?> arg0) {
		// TODO Auto-generated method stub
		map.setImageResource(currentMap);

	}
}
